package com.shop.expandablelist;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ExpandableListView cart_recy;
    private CheckBox all_cb;
    private TextView txt_price;
    private CartAdapter cartAdapter;
    private Button btn_jiesuan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }


    private void initView() {
        cart_recy = (ExpandableListView) findViewById(R.id.cart_recy);
        all_cb = (CheckBox) findViewById(R.id.all_cb);
        txt_price = (TextView) findViewById(R.id.txt_price);
        btn_jiesuan = (Button) findViewById(R.id.btn_jiesuan);
        btn_jiesuan.setOnClickListener(this);
        all_cb.setOnClickListener(this);
    }

    private void initData() {
        List<DataBean> list = new ArrayList<>();
        List<DataBean.ListBean> listBeans = new ArrayList<>();
        listBeans.add(new DataBean.ListBean("", "春季宽松直筒裤", 10, 1, 0));
        listBeans.add(new DataBean.ListBean("", "春季宽松直筒裤", 20, 1, 0));
        list.add(new DataBean("春季服饰店铺", listBeans));
        cartAdapter = new CartAdapter(list);
        cartAdapter.setOnCartListener(new CartAdapter.OnCartListener() {
            @Override
            public void onCartSjChecked(int groupPosition) {
                /**
                 * 点击商家的时候回调
                 */
                boolean allSjShopselected = cartAdapter.isAllSjShopselected(groupPosition);
                cartAdapter.SjSelected(groupPosition, !allSjShopselected);
                cartAdapter.notifyDataSetChanged();
                notifyButtomNumber();
            }

            @Override
            public void onCartShopChecked(int groupPosition, int childPosition) {
                /**
                 * 点击商品的时候回调
                 */
                cartAdapter.ShopSelected(groupPosition, childPosition);
                cartAdapter.notifyDataSetChanged();
                notifyButtomNumber();
            }

            @Override
            public void onCartSubviewChecked(int groupPosition, int childPosition, int number) {
                /**
                 * 加减器回调
                 */
                cartAdapter.subviewSelected(groupPosition, childPosition, number);
                cartAdapter.notifyDataSetChanged();
                notifyButtomNumber();
            }
        });
        cart_recy.setAdapter(cartAdapter);

        // 展开二级列表
        for (int x = 0; x < list.size(); x++) {
            cart_recy.expandGroup(x);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_jiesuan:

                break;
            case R.id.all_cb:
                boolean bottomAllselected = cartAdapter.isBottomAllselected();
                cartAdapter.ButtomSelected(!bottomAllselected);
                cartAdapter.notifyDataSetChanged();
                notifyButtomNumber();
                break;
        }
    }

    private void notifyButtomNumber() {
        //去判断是否所有的商品都被选中
        boolean bottomAllselected = cartAdapter.isBottomAllselected();
        all_cb.setChecked(bottomAllselected);
        //计算总计
        float prices = cartAdapter.ShopPrices();
        txt_price.setText("总计" + prices);

        int number = cartAdapter.ShopNumber();
        btn_jiesuan.setText("去结算(" + number + ")");
    }
}
